package com.ssm.web;

import com.ssm.entity.File;
import com.ssm.entity.Log;
import com.ssm.entity.User;
import com.ssm.service.FileService;
import com.ssm.service.LogService;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by bai on 2017/7/26.
 */

@Controller
@RequestMapping("/user")
public class UserController {

    //用户业务层实例化
    @Autowired(required = false)
    private UserService userService;

    @Autowired(required = false)
    private FileService fileService;

    @Autowired(required = false)
    private LogService logService;



    /**
     * 登录页面
     *
     * */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    private String loginPage(User user,Model model) {
        return "login";
    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    private String login(@ModelAttribute("form") User user, Model model,HttpServletRequest request) throws Exception
    {
        //System.out.println(user.getUsername());
        user = userService.checkLogin(user.getUsername(), user.getUserPwd());
        if(user != null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("username",user.getUsername());
            session.setAttribute("user",user);
            model.addAttribute("user",user);

            System.out.print(user.getUsername());

            if( user.getUsername().equals("admin") ) {

                //查询数据库中所有的日志*/
                List<File> fileList = fileService.queryAll();
                System.out.print(fileList);
                model.addAttribute("fileList", fileList);
            }
            else
            {
                System.out.print("这里是else循环");
                List<File> fileList = fileService.queryByUserName(user.getUsername());
                model.addAttribute("fileList", fileList);
            }

            Log log=new Log();
            log.setUserId(user.getId());
            String opertionstring="用户"+user.getUsername()+"登录了系统";
            log.setOperations(opertionstring);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            Timestamp timestamp =new Timestamp(System.currentTimeMillis());
            log.setTime(timestamp);
            logService.addLog(log);

            return "welcome";//路径 WEB-INF/pages/welcome.jsp
        }
        return "failToLogin";
    }



    /**
     * 注册页面
     *
     * */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    private String registerPage(User user,Model model) {
        return "register";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    private String register(@ModelAttribute("form") User user, Model model, HttpServletRequest request) throws Exception
    {
        boolean flag = false;
        flag = userService.checkRegister(user.getUsername());
        if (flag)
        {
            HttpSession session = request.getSession();
            session.setAttribute("username",user.getUsername());

            //用model存储页面间传递的变量
            model.addAttribute(user);

            userService.addUser(user);

            return "welcome";
        }

        return "failregister";

    }


    /**
     * （管理员）用户管理页面
     *
     * */
    @RequestMapping(value = "/allUsers", method = RequestMethod.GET)
    private String allUsersPage(User user,Model model) {
        return "allUsers";
    }




    /**
     * 测试文件关键字查询页面
     *
     * */
    @RequestMapping(value = "/testFileSearch", method = RequestMethod.GET)
    private String testFileSearchPage(User user, Model model) {
        return "testFileSearch";
    }


    /**
     * 管理员进行用户的模糊查找页面
     *
     *
     * */
    @RequestMapping(value = "/findUser", method = RequestMethod.GET)
    private String showUserPage(User user, Model model) {
        return "findUser";
    }
    @RequestMapping(value="/findUser" ,method = RequestMethod.POST)
    public String lookTest( HttpServletRequest request, Model model) throws Exception {

        //获取关键字
        String keyName;
        keyName=request.getParameter("keyName");
        //控制台输出关键字
        System.out.println(keyName);
        List<User> userList = userService.queryByKeyName(keyName);

        //控制台输出所有符合条件的用户的用户名
        for(int i = 0; i<userList.size();i++)
            System.out.print(userList.get(i).getUsername());
        model.addAttribute("fileList", userList);

        return "showUser";
    }


    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    private String welcomePageGet(User user, Model model, HttpServletRequest request) {
        return "welcome";
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.POST)
    public String welcomePage(User user,Model model, HttpServletRequest request ){

        HttpSession session = request.getSession();
        session.setAttribute("user",user);

        /**
         * 判断当前用户是否为管理员
         * 是则查询所有文档
         * 否则查询用户名下的文档
         * */
        //User testUser = userService.queryById(1);
        System.out.print(user.getUsername());

       /* if(user.getUsername().equals("admin")) {

            //查询数据库中所有的日志
            List<File> fileList = fileService.queryAll();
            System.out.print(fileList);
            model.addAttribute("fileList", fileList);
        }
        else
        {
            List<File> fileList = fileService.queryByUserName(user.getUsername());
            model.addAttribute("fileList", fileList);
        }
*/
        return "welcome";
    }
}
